<?php
require_once( '../../libs/smarty.inc' );
require_once( '../../libs/smt_variables.php' );
require_once( '../../libs/new_smt_variables.php' );
require_once( '../../libs/Class_DB.php' );
require_once( '../../libs/Class_ERROR.php' );
require_once( '../../libs/login.inc.php' );
require_once( '../../libs/page.php' );

if( !in_array( "M", $_SESSION['ADMIN_LOGIN']['AUTH_CD'] ) ) exit;
if( $_SERVER['REQUEST_METHOD'] == 'GET'){
	$current=1;
	if( isset( $_GET['page'] ) && is_numeric( $_GET['page'] ) ){
		$current = $_GET['page'];
		$st = ($current -1) * PAGE_SIZE;
	}else{
		$current = 1;
		$st = 0;
	}

	$forms = $_GET;
	$company_name = $_GET["company_name"];
	$katakana = $_GET["katakana"];
	$branch_name = $_GET["branch_name"];
	$sal_manager = $_GET["sal_manager"];
	$r_date1 = $_GET["r_date1"];
	$r_date2 = $_GET["r_date2"];
	$u_date1 = $_GET["u_date1"];
	$u_date2 = $_GET["u_date2"];
	
	$price_no = $_GET['price_no'];
	$pro_name = $_GET['pro_name'];
	$category = $_GET['category'];
	$m_date1 = $_GET['m_date1'];
	$m_date2 = $_GET['m_date2'];
	$u_date3 = $_GET["u_date3"];
	$u_date4 = $_GET["u_date4"];
	$producer = $_GET['producer'];
	$status1 = $_GET['status1'];
	$status2 = $_GET['status2'];
	$status3 = $_GET['status3'];
	$status4 = $_GET['status4'];
	$status5 = $_GET['status5'];
	$status6 = $_GET['status6'];
	$e_date1 = $_GET['e_date1'];
	$e_date2 = $_GET['e_date2'];
	$u_date5 = $_GET["u_date5"];
	$u_date6 = $_GET["u_date6"];
	
	
	//SQLクエリー
	$sql1="SELECT A.*,B.* ";
	$sql2="SELECT count(*) AS cnt ";
	
	$sql = "FROM p_consumer A inner join p_consumer_head B on  A.consumer_id = B.consumer_id  ";
	if($category != null){
		$sql = $sql."  inner join (select D.type,C.price_no from p_consumer_detail C  ";
		$sql = $sql."inner join p_shop D on C.shop_id=D.shop_id  group by C.price_no, D.type ) D  on D.price_no=B.price_no   ";
	}
	$sql =  $sql." WHERE 1 = 1 ";
	
	$query = "";
	//echo $sql;
	if($company_name != null){
		$sql = $sql." and A.company_name like '%".$company_name."%'";
		$query .="&company_name=$company_name";
	}
	if($katakana != null){
		$sql = $sql." and A.katakana like '%".$katakana."%'";
		$query .="&katakana=$katakana";
	}
	if($branch_name != null){
		$sql = $sql." and A.branch_name like '%".$branch_name."%'";
		$query .="&branch_name=$branch_name";
	}
	if($sal_manager != null){
		$sql = $sql." and A.sal_manager like '%".$sal_manager."%'";
		$query .="&sal_manager=$sal_manager";
	}
	if($r_date1 != null && $r_date2 != null){
		$sql = $sql." and A.r_date BETWEEN '".$r_date1."' and '".$r_date2."'";
		$query .="&r_date1=$r_date1&r_date2=$r_date2";
	}
	if($r_date1 != null && $r_date2 == null){
		$sql = $sql." and A.r_date >= '".$r_date1."'";
		$query .="&r_date1=$r_date1";
	}
	if($r_date1 == null && $r_date2 != null){
		$sql = $sql." and A.r_date < '".$r_date2."'";
		$query .="&r_date2=$r_date2";
	}
	if($u_date1 != null && $u_date2 != null){
		$sql = $sql." and A.u_date BETWEEN '".$u_date1."' and '".$u_date2."'";
		$query .="&u_date1=$u_date1&u_date2=$u_date2";
	}
	if($u_date1 != null && $u_date2 == null){
		$sql = $sql." and A.u_date >= '".$u_date1."'";
		$query .="&u_date1=$u_date1";
	}
	if($u_date1 == null && $u_date2 != null){
		$sql = $sql." and A.u_date < '".$u_date2."'";
		$query .="&u_date2=$u_date2";
	}
	
	
	if($price_no != null){
		$sql = $sql." and B.price_no like '".$price_no."%'";
		$query .="&price_no=$price_no";
	}
	if($pro_name != null){
		$sql = $sql." and B.pro_name like '%".$pro_name."%'";
		$query .="&pro_name=$pro_name";
	}
	if($category != null){
		$sql = $sql." and D.type like '%".$category."%'";
		$query .="&category=$category";
	}
	if($m_date1 != null && $m_date2 != null){
		$sql = $sql." and B.m_date BETWEEN '".$m_date1."' and '".$m_date2."'";
		$query .="&m_date1=$m_date1&m_date2=$m_date2";
	}
	if($m_date1 != null && $m_date2 == null){
		$sql = $sql." and B.m_date >= '".$m_date1."'";
		$query .="&m_date1=$m_date1";
	}
	if($m_date1 == null && $m_date2 != null){
		$sql = $sql." and B.m_date <= '".$m_date2."'";
		$query .="&m_date2=$m_date2";
	}
	if($u_date3 != null && $u_date4 != null){
		$sql = $sql." and B.u_date BETWEEN '".$u_date3."' and '".$u_date4."'";
		$query .="&u_date3=$u_date3&u_date4=$u_date4";
	}
	if($u_date3 != null && $u_date4 == null){
		$sql = $sql." and B.u_date >= '".$u_date3."'";
		$query .="&u_date3=$u_date3";
	}
	if($u_date3 == null && $u_date4 != null){
		$sql = $sql." and B.u_date <= '".$u_date4."'";
		$query .="&u_date4=$u_date4";
	}
	if($producer != null){
		$sql = $sql." and B.producer like '%".$producer."%'";
		$query .="&producer=$producer";
	}
	$status = array();
	if($status1 != null){
		$status[] = $status1;
		$query .="&status1=$status1";
	}
	if($status2 != null){
		$status[] = $status2;
		$query .="&status2=$status2";
	}
	if($status3 != null){
		$status[] = $status3;
		$query .="&status3=$status3";
	}
	if($status4 != null){
		$status[] = $status4;
		$query .="&status4=$status4";
	}
	if($status5 != null){
		$status[] = $status5;
		$query .="&status5=$status5";
	}
	if($status6 != null){
		$status[] = $status6;
		$query .="&status6=$status6";
	}
	if(count($status)>0){
		$sql = $sql." and B.status in (".implode(",", $status).")";
	}
	if($e_date1 != null && $e_date2 != null){
		$sql = $sql." and B.e_date BETWEEN '".$e_date1."' and '".$e_date2."'";
		$query .="&e_date1=$e_date1&e_date2=$e_date2";
	}
	if($e_date1 != null && $e_date2 == null){
		$sql = $sql." and B.e_date >= '".$e_date1."'";
		$query .="&e_date1=$e_date1";
	}
	if($e_date1 == null && $e_date2 != null){
		$sql = $sql." and B.e_date <= '".$e_date2."'";
		$query .="&e_date2=$e_date2";
	}
	if($u_date5 != null && $u_date6 != null){
		$sql = $sql." and B.ok_date BETWEEN '".$u_date5."' and '".$u_date6."'";
		$query .="&u_date5=$u_date5&u_date6=$u_date6";
	}
	if($u_date5 != null && $u_date6 == null){
		$sql = $sql." and B.ok_date >= '".$u_date5."'";
		$query .="&u_date5=$u_date5";
	}
	if($u_date5 == null && $u_date6 != null){
		$sql = $sql." and B.ok_date <= '".$u_date6."'";
		$query .="&u_date6=$u_date6";
	}
	
	$staff_id = $_SESSION['ADMIN_LOGIN']['STAFF_ID'];
	
	$sql = $sql." and B.staff_id=$staff_id  order by B.u_date desc";
	
	$sql1 = $sql1.$sql." LIMIT $st, ".PAGE_SIZE;
	$sql2 = $sql2.$sql;
	
	$db = new Class_DB;
	
	$res = $db->query($sql1);

	while( $row = mysql_fetch_array($res,MYSQL_ASSOC) ){
	
		//1作成中  2 承認依頼中    3承認済み   4 承認却下    5客先OK  6客先NG
		if ($row['status']==5){
			$opt = "OK";
	
		}elseif ($row['status']==6){
			$opt = "NG";
	
		}
		$row['opt'] = $opt;
		$row['r_date']=$row['r_date']==NULL?"":date('Y/m/d',strtotime($row['r_date']));
		$row['u_date']=$row['u_date']==NULL?"":date('Y/m/d',strtotime($row['u_date']));
		$row['m_date']=$row['m_date']==NULL?"":date('Y/m/d',strtotime($row['m_date']));
		$row['e_date']=$row['e_date']==NULL?"":date('Y/m/d',strtotime($row['e_date']));
		$resultlist[] = $row;
	
	
	}
	
	$cnt = 0;
	$db->query( $sql2 );
	$row = $db->fetch();
	$cnt = $row['cnt'];
	
	$smarty->assign('page', getPage($current, $cnt,$query));
	$smarty->assign('cnt', $cnt);
	$smarty->assign('resultlist', $resultlist);
	if( isset( $forms ) ) $smarty->assign('forms', $forms);
	
	$sql = "SELECT * FROM M_STAFF";
	$res = $db->query($sql);
	while( $row = mysql_fetch_array($res,MYSQL_ASSOC) ){
		$staff[$row['staff_id']] = $row['staff_name'];
			
	}
	$smarty->assign('staff', $staff);
	
	
}

$menu_html = file_get_contents( $_SERVER['DOCUMENT_ROOT'].'/templates/web-admin/menu.html' );

$smarty->assign('menu_html', $menu_html);

$smarty->display('web-admin/price_check.html');


?>